package com.ruoyi.wzgl.wzsq.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.infoRunTaskData.domain.InfoRunTaskData;
import com.ruoyi.infoRunTaskData.service.IInfoRunTaskDataService;
import com.ruoyi.wzgl.wzsq.domain.Wzsq;
import com.ruoyi.wzgl.wzsq.domain.WzsqSqmx;
import com.ruoyi.wzgl.wzsq.service.IWzsqService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 物资申请Controller
 *
 * @author huacai
 * @date 2024-09-29
 */
@RestController
@RequestMapping("/wzgl/wzsq")
public class WzsqController extends BaseController {
    @Resource
    private IWzsqService wzsqService;

    @Resource
    private IInfoRunTaskDataService iInfoRunTaskDataService;

    /**
     * 获取当前登录用户ID
     */
    @GetMapping("/getUserById")
    public AjaxResult getUserById() {
        Long userId = getUserId();
        return success(userId);
    }

    /**
     * 查询物资申请列表
     */
    @PreAuthorize("@ss.hasPermi('wzgl:wzsq:list')")
    @GetMapping("/list")
    public TableDataInfo list(Wzsq wzsq) {
        startPage();
        List<Wzsq> list = wzsqService.selectWzsqList(wzsq);
        return getDataTable(list);
    }

    /**
     * 导出物资申请列表
     */
    @PreAuthorize("@ss.hasPermi('wzgl:wzsq:export')")
    @Log(title = "物资申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Wzsq wzsq) {
        List<Wzsq> list = wzsqService.selectWzsqList(wzsq);
        ExcelUtil<Wzsq> util = new ExcelUtil<Wzsq>(Wzsq.class);
        util.exportExcel(response, list, "物资申请数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Wzsq> util = new ExcelUtil<Wzsq>(Wzsq.class);
        util.importTemplateExcel(response, "物资申请数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "物资申请", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('wzgl:wzsq:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Wzsq> util = new ExcelUtil<Wzsq>(Wzsq.class);
        InputStream inputStream = file.getInputStream();
        List<Wzsq> list = util.importExcel(inputStream);
        inputStream.close();
        int count = wzsqService.batchInsertWzsq(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取物资申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('wzgl:wzsq:query')")
    @GetMapping(value = "/{wzsqId}")
    public AjaxResult getInfo(@PathVariable("wzsqId") Integer wzsqId) {
        return success(wzsqService.selectWzsqByWzsqId(wzsqId));
    }

    /**
     * 新增物资申请
     */
    @PreAuthorize("@ss.hasPermi('wzgl:wzsq:add')")
    @Log(title = "物资申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Wzsq wzsq) {
        wzsq.setUserId(Math.toIntExact(getUserId()));
        wzsq.setDeptId(Math.toIntExact(getDeptId()));
        wzsq.setCreateBy(getUsername());
        int i = wzsqService.insertWzsq(wzsq);
        //向流程业务表中加入数据
        InfoRunTaskData infoRunTaskData = new InfoRunTaskData();
        infoRunTaskData.setBusinessKey(String.valueOf(wzsq.getWzsqId()));
        infoRunTaskData.setBusinessType(13); //业务模块: 物资管理-物资申请
        infoRunTaskData.setBusinessTitle(wzsq.getGqName() + '-' + wzsq.getSqName()); //任务名称: 申请工区 + 申请人
        infoRunTaskData.setApplyUser(SecurityUtils.getLoginUser().getUser().getNickName());//申请人

        iInfoRunTaskDataService.insertInfoRunTaskData(infoRunTaskData);
        return toAjax(i);
    }

    /**
     * 修改物资申请
     */
    @PreAuthorize("@ss.hasPermi('wzgl:wzsq:edit')")
    @Log(title = "物资申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Wzsq wzsq) {
        //修改业务中间表关系数据
        InfoRunTaskData infoRunTaskData = new InfoRunTaskData();
        infoRunTaskData.setBusinessTitle(wzsq.getGqName() + '-' + wzsq.getSqName()); //任务名称: 申请工区 + 申请人
        infoRunTaskData.setBusinessKey(String.valueOf(wzsq.getWzsqId()));
        infoRunTaskData.setBusinessType(13); //业务模块: 物资管理-物资申请
        iInfoRunTaskDataService.updateInfoRunTaskDatas(infoRunTaskData); //注意updateInfoRunTaskDatas

        return toAjax(wzsqService.updateWzsq(wzsq));
    }

    /**
     * 删除物资申请
     */
    @PreAuthorize("@ss.hasPermi('wzgl:wzsq:remove')")
    @Log(title = "物资申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{wzsqIds}")
    public AjaxResult remove(@PathVariable Integer[] wzsqIds) {
        //同时删除业务关系数据
        for (Integer wzsqId : wzsqIds) {
            Wzsq wzsq = wzsqService.selectWzsqByWzsqId(wzsqId);
            iInfoRunTaskDataService.deleteData(String.valueOf(wzsq.getWzsqId()), 13);//业务模块: 物资管理-物资申请
        }
        return toAjax(wzsqService.deleteWzsqByWzsqIds(wzsqIds));
    }

    /**
     * 修改物资申请明细
     */
    @Log(title = "物资申请明细", businessType = BusinessType.UPDATE)
    @PutMapping("/updateWzsqSqmx")
    public AjaxResult updateWzsqSqmx(@RequestBody WzsqSqmx wzsqSqmx) {
        return toAjax(wzsqService.updateWzsqSqmx(wzsqSqmx));
    }

}
